﻿using PlaneClean.Models;
using PlaneCleanEntity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// plc操作日志
    /// </summary>
    public class HandleRecordController : ManagerController
    {
        /// <summary>
        /// 查询操作记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<v_t_handle_record> List(PageListRequest<v_t_handle_record> request)
        {
            if (request.pageIndex <= 0) request.pageIndex = 1;
            if (request.pageSize <= 0) request.pageSize = int.MaxValue;
            int total = 0;
            ListResponse<v_t_handle_record> response = new ListResponse<v_t_handle_record>();
            response.bodyList = new List<v_t_handle_record>();
            response.result = "NG";
            var sugar = DbFactory.dbFactory.Db.Queryable<t_handle_record>()
                .Where(a => a.h_id > 0);
            if (!string.IsNullOrEmpty(request.body.h_operator))
            {
                sugar.Where(a => a.h_operator.Contains(request.body.h_operator));
            }
            if (request.body.record_time != null && request.body.record_time.Count > 0)
            {
                sugar.Where(a => Convert.ToDateTime(request.body.record_time[0])
                < a.h_handle_time && a.h_handle_time < Convert.ToDateTime(request.body.record_time[1]));
            }
            sugar.OrderBy(a => a.h_handle_time, OrderByType.Desc);
            //加上where 加快查询速度 (where 中使用索引条件)
            List<v_t_handle_record> hrList = sugar.Select<v_t_handle_record>().ToPageList(request.pageIndex, request.pageSize, ref total);
            foreach (var  hr in hrList)
            {
                hr.planeName = DbFactory.dbFactory.Db.Queryable<t_plane>()
                    .Where(a => a.p_id == hr.h_pId).Select(a => a.p_name).First();
            }
            response.bodyList.AddRange(hrList);
            response.total = total;

            response.result = "OK";
            return response;
        }
    }
}